#!/bin/bash

#zookeeper压缩文件名
zookeeper_file_name=$(ls ./ | grep zookeeper-.*tar.gz)
#zookeeper解压后文件名(截取字符串)
zookeeper_dir=${zookeeper_file_name%%.tar.gz}
#zookeeper日志目录
zookeeper_log="/tmp/zookeeper"
machine1="/1.machine"
machine2="/2.machine"
machine3="/3.machine"
echo "------------------------开始读取zookeeper文件压缩包:${zookeeper_file_name}------------------------"

echo "------------------------判断文件夹${zookeeper_dir}是否存在------------------------"
if [ -d "./${zookeeper_dir}" ];
then
    echo "------------------------文件夹${zookeeper_dir}已存在,执行删除------------------------"
    rm -rf "./${zookeeper_dir}"
else
    echo "------------------------文件夹${zookeeper_dir}不存在,无需删除------------------------"
fi

echo "------------------------开始解压zookeeper文件压缩包:${zookeeper_file_name}------------------------"
tar -zxvf ${zookeeper_file_name}
echo "------------------------完成解压zookeeper文件压缩包:${zookeeper_file_name}------------------------"

# 创建log目录
if [ -d "${zookeeper_log}" ];
then
    echo "------------------------${zookeeper_log}目录已存在,无需创建------------------------"
else
    echo "------------------------${zookeeper_log}目录不存在,创建------------------------"
    mkdir "${zookeeper_log}"
fi
# 创建配置文件
cp "./${zookeeper_dir}/conf/zoo_sample.cfg" "./${zookeeper_dir}/conf/1.zoo.cfg"
cp "./${zookeeper_dir}/conf/zoo_sample.cfg" "./${zookeeper_dir}/conf/2.zoo.cfg"
cp "./${zookeeper_dir}/conf/zoo_sample.cfg" "./${zookeeper_dir}/conf/3.zoo.cfg"
# 创建目录/tmp/zookeeper/1.machine 目录
# 创建目录/tmp/zookeeper/2.machine 目录
# 创建目录/tmp/zookeeper/3.machine 目录
if [ -d "${zookeeper_log}/${machine1}" ];
then
    echo "------------------------${zookeeper_log}/${machine1}目录已存在,无需创建------------------------"
else
    echo "------------------------${zookeeper_log}/${machine1}目录不存在,创建------------------------"
    mkdir "${zookeeper_log}/${machine1}"
fi

if [ -d "${zookeeper_log}/${machine2}" ];
then
    echo "------------------------${zookeeper_log}/${machine2}目录已存在,无需创建------------------------"
else
    echo "------------------------${zookeeper_log}/${machine2}目录不存在,创建------------------------"
    mkdir "${zookeeper_log}/${machine2}"
fi

if [ -d "${zookeeper_log}/${machine3}" ];
then
    echo "------------------------${zookeeper_log}/${machine3}目录已存在,无需创建------------------------"
else
    echo "------------------------${zookeeper_log}/${machine3}目录不存在,创建------------------------"
    mkdir "${zookeeper_log}/${machine3}"
fi

# 创建myid文件,先删除
if [ -e "${zookeeper_log}/${machine1}/myid" ];
then
    rm -rf "${zookeeper_log}/${machine1}/myid"
fi

if [ -e "${zookeeper_log}/${machine2}/myid" ];
then
    rm -rf "${zookeeper_log}/${machine2}/myid"
fi

if [ -e "${zookeeper_log}/${machine3}/myid" ];
then
    rm -rf "${zookeeper_log}/${machine3}/myid"
fi

touch ${zookeeper_log}/${machine1}/myid
echo 1 >> ${zookeeper_log}/${machine1}/myid
touch ${zookeeper_log}/${machine2}/myid
echo 2 >> ${zookeeper_log}/${machine2}/myid
touch ${zookeeper_log}/${machine3}/myid
echo 3 >> ${zookeeper_log}/${machine3}/myid

# 修改配置文件
sed  -i  's/dataDir=\/tmp\/zookeeper/dataDir=\/tmp\/zookeeper\/1.machine/g'  "./${zookeeper_dir}/conf/1.zoo.cfg"
sed  -i  's/dataDir=\/tmp\/zookeeper/dataDir=\/tmp\/zookeeper\/2.machine/g'  "./${zookeeper_dir}/conf/2.zoo.cfg"
sed  -i  's/dataDir=\/tmp\/zookeeper/dataDir=\/tmp\/zookeeper\/3.machine/g'  "./${zookeeper_dir}/conf/3.zoo.cfg"
sed  -i  's/clientPort=2181/clientPort=2181/g'  "./${zookeeper_dir}/conf/1.zoo.cfg"
sed  -i  's/clientPort=2181/clientPort=2182/g'  "./${zookeeper_dir}/conf/2.zoo.cfg"
sed  -i  's/clientPort=2181/clientPort=2183/g'  "./${zookeeper_dir}/conf/3.zoo.cfg"

sed  -i '$a\server.1=127.0.0.1:20881:30881' "./${zookeeper_dir}/conf/1.zoo.cfg"
sed  -i '$a\server.2=127.0.0.1:20882:30882' "./${zookeeper_dir}/conf/1.zoo.cfg"
sed  -i '$a\server.3=127.0.0.1:20883:30883' "./${zookeeper_dir}/conf/1.zoo.cfg"

sed  -i '$a\server.1=127.0.0.1:20881:30881' "./${zookeeper_dir}/conf/2.zoo.cfg"
sed  -i '$a\server.2=127.0.0.1:20882:30882' "./${zookeeper_dir}/conf/2.zoo.cfg"
sed  -i '$a\server.3=127.0.0.1:20883:30883' "./${zookeeper_dir}/conf/2.zoo.cfg"

sed  -i '$a\server.1=127.0.0.1:20881:30881' "./${zookeeper_dir}/conf/3.zoo.cfg"
sed  -i '$a\server.2=127.0.0.1:20882:30882' "./${zookeeper_dir}/conf/3.zoo.cfg"
sed  -i '$a\server.3=127.0.0.1:20883:30883' "./${zookeeper_dir}/conf/3.zoo.cfg"

#echo "------------------------开始启动zookeeper集群------------------------"
#sh ./${zookeeper_dir}/bin/zkServer.sh start ./${zookeeper_dir}/conf/1.zoo.cfg
#sh ./${zookeeper_dir}/bin/zkServer.sh start ./${zookeeper_dir}/conf/2.zoo.cfg
#sh ./${zookeeper_dir}/bin/zkServer.sh start ./${zookeeper_dir}/conf/3.zoo.cfg
#
#sh ./${zookeeper_dir}/bin/zkServer.sh status ./${zookeeper_dir}/conf/1.zoo.cfg
#sh ./${zookeeper_dir}/bin/zkServer.sh status ./${zookeeper_dir}/conf/2.zoo.cfg
#sh ./${zookeeper_dir}/bin/zkServer.sh status ./${zookeeper_dir}/conf/3.zoo.cfg
#echo "------------------------结束启动zookeeper集群------------------------"


#echo "------------------------开始停止zookeeper集群------------------------"
#sh ./${zookeeper_dir}/bin/zkServer.sh stop ./${zookeeper_dir}/conf/1.zoo.cfg
#sh ./${zookeeper_dir}/bin/zkServer.sh stop ./${zookeeper_dir}/conf/2.zoo.cfg
#sh ./${zookeeper_dir}/bin/zkServer.sh stop ./${zookeeper_dir}/conf/3.zoo.cfg
#
#echo "------------------------结束停止zookeeper集群------------------------"